<template>
  <el-radio-group :value="selectValue" :size="size" :disabled="readonly" @input="handleInput">
    <el-radio
      v-for="item in dictionaryItemList"
      :key="item.code"
      :label="item.code"
    >{{ item.label }}</el-radio>
  </el-radio-group>
</template>

<script>

export default {
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      required: false,
      default: ''
    },
    code: {
      type: String,
      default: ''
    },
    readonly: {
      type: Boolean,
      required: false,
      default: false
    },
    size: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      selectValue: '',
      dictionaryItemList: []

    }
  },
  watch: {
    value: {
      immediate: true,
      handler: 'handleValue'
    }
    // code: {
    //   immediate: true,
    //   handler: 'handleCode'
    // }
  },
  mounted() {
    this.dictionaryItemList = []
    this.$api.system.dictionaryType.getItem(this.code).then(res => {
      this.dictionaryItemList = res.data
    })
  },
  methods: {
    handleInput: function (selectedValue) {
      this.$emit('change', selectedValue)
    },
    handleValue: function () {
      this.selectValue = this.value
    },
    handleCode() {
      if (this.code) {
        this.dictionaryItemList = []
        this.$api.system.dictionaryType.getItem(this.code).then(res => {
          this.dictionaryItemList = res.data
        })
      }
    }

  }
}
</script>
